import cv2


def play_video(video_path):
    # 打开视频文件
    cap = cv2.VideoCapture('video.mp4')

    # 检查视频是否成功打开
    if not cap.isOpened():
        print("Error: Could not open video.")
        return

        # 循环读取视频的每一帧
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            # 如果读取不到帧（已经播放到视频末尾），则退出循环
            break

            # 显示当前帧
        cv2.imshow('Video', frame)

        # 等待按键，如果按下'q'键则退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

            # 释放视频文件和销毁所有窗口
    cap.release()
    cv2.destroyAllWindows()


# 替换为你的视频文件路径
video_file_path = "path_to_your_video_file.mp4"

# 调用函数播放视频
play_video(video_file_path)